﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package GameMode {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Classes.*;
    import Display.*;
    import Modules.guide.ui.*;
    import Engine.*;
    import Modules.stats.types.*;

    public class GMPlaceBridge extends GMPlaceMapResource {

        protected var m_area:Sprite;
        protected var m_freeArea:Sprite;
        protected var m_endSquare:Sprite;
        protected var m_ghostBridge:Sprite;
        protected var selectedBridge:Bridge;
        protected var placeItem:Item;
        protected var bridgeItem:ItemDefinitionBridge;

        public function GMPlaceBridge(_arg1:String, _arg2:Class, _arg3:Boolean=false){
            super(_arg1, _arg2, _arg3);
            if (this.bridgeItem == null){
                this.placeItem = Global.gameSettings.getItemByName(_arg1);
                this.bridgeItem = this.placeItem.bridge;
            };
        }
        private function setupSelectedBridge():void{
            var _local1:ConstructionSite;
            if (m_selectedObject == null){
                return;
            };
            if ((m_selectedObject is Bridge)){
                this.selectedBridge = (m_selectedObject as Bridge);
            };
            if ((m_selectedObject is ConstructionSite)){
                _local1 = (m_selectedObject as ConstructionSite);
                if (_local1.targetClass == Bridge){
                    this.selectedBridge = new Bridge(_local1.targetName);
                };
            };
        }
        private function getBridgeImage():DisplayObject{
            if (this.selectedBridge == null){
                return (null);
            };
            if (this.selectedBridge.isCurrentImageLoading()){
                return (null);
            };
            var _local1:ItemImageInstance = this.selectedBridge.getGhostImage();
            return ((_local1.image as DisplayObject));
        }
        private function get playerDoesNotOwnTargetSquare():Boolean{
            var _local1:Vector2 = new Vector2(this.bridgeItem.targetSquare.x, this.bridgeItem.targetSquare.y);
            var _local2:Vector2 = new Vector2((this.bridgeItem.targetSquare.x + this.placeItem.sizeX), this.bridgeItem.targetSquare.y);
            var _local3:Vector2 = new Vector2((this.bridgeItem.targetSquare.x + this.placeItem.sizeX), (this.bridgeItem.targetSquare.y + this.placeItem.sizeX));
            var _local4:Vector2 = new Vector2(this.bridgeItem.targetSquare.x, (this.bridgeItem.targetSquare.y + this.placeItem.sizeX));
            if (Global.world.m_territory.pointInTerritory(_local1.x, _local1.y)){
                return (false);
            };
            if (Global.world.m_territory.pointInTerritory(_local2.x, _local2.y)){
                return (false);
            };
            if (Global.world.m_territory.pointInTerritory(_local3.x, _local3.y)){
                return (false);
            };
            if (Global.world.m_territory.pointInTerritory(_local4.x, _local4.y)){
                return (false);
            };
            return (true);
        }
        private function handleNotExpandedToBridgeTarget():void{
            Global.world.centerOnIsoPosition(this.bridgeItem.targetSquare, 0);
            var _local1:int = (Global.stage.stageWidth / 2);
            var _local2:int = (Global.stage.stageHeight / 2);
            UI.displayStatus(ZLoc.t("Main", "BuyExpansionFirstRed"), _local1, _local2);
            var _local3:Array = Global.world.getActiveGameModes();
            var _local4:int;
            while (_local4 < _local3.length) {
                Global.world.removeGameMode(_local3[_local4]);
                _local4++;
            };
            Global.world.addGameMode(new GMPlay());
        }
        override public function enableMode():void{
            super.enableMode();
            if (this.selectedBridge == null){
                this.setupSelectedBridge();
            };
            if (this.playerDoesNotOwnTargetSquare){
                this.handleNotExpandedToBridgeTarget();
                return;
            };
            Global.guide.notify("BridgeGuide");
        }
        private function resolveSpriteDisable(_arg1:Sprite):void{
            if (_arg1){
                if (_arg1.parent != null){
                    GlobalEngine.viewport.overlayBase.removeChild(_arg1);
                };
            };
        }
        override public function disableMode():void{
            super.disableMode();
            this.resolveSpriteDisable(this.m_area);
            this.resolveSpriteDisable(this.m_freeArea);
            this.resolveSpriteDisable(this.m_endSquare);
            this.resolveSpriteDisable(this.m_ghostBridge);
        }
        protected function drawFreeWaterArea(_arg1:Rectangle, _arg2:uint):void{
            var _local3:Boolean;
            if (!this.m_freeArea){
                this.m_freeArea = new Sprite();
                _local3 = true;
            };
            var _local4:Vector2 = IsoMath.tilePosToPixelPos(_arg1.x, _arg1.y);
            var _local5:Vector2 = IsoMath.tilePosToPixelPos(_arg1.x, _arg1.bottom);
            var _local6:Vector2 = IsoMath.tilePosToPixelPos(_arg1.right, _arg1.bottom);
            var _local7:Vector2 = IsoMath.tilePosToPixelPos(_arg1.right, _arg1.y);
            var _local8:Number = 2;
            this.m_freeArea.graphics.clear();
            this.m_freeArea.graphics.lineStyle(_local8, _arg2);
            this.m_freeArea.graphics.moveTo(_local6.x, _local6.y);
            this.m_freeArea.graphics.lineTo(_local7.x, _local7.y);
            this.m_freeArea.graphics.lineTo(_local4.x, _local4.y);
            this.m_freeArea.graphics.lineTo(_local5.x, _local5.y);
            this.m_freeArea.graphics.lineTo(_local6.x, _local6.y);
            if (_local3){
                GlobalEngine.viewport.overlayBase.addChild(this.m_freeArea);
            };
        }
        protected function clearFreeWaterArea():void{
            var _local1:Boolean;
            if (!this.m_freeArea){
                this.m_freeArea = new Sprite();
                _local1 = true;
            };
            this.m_freeArea.graphics.clear();
            if (_local1){
                GlobalEngine.viewport.overlayBase.addChild(this.m_freeArea);
            };
        }
        protected function drawGhostBridge():void{
            var _local1:Boolean;
            if (!this.m_ghostBridge){
                this.m_ghostBridge = new Sprite();
                _local1 = true;
            };
            var _local2:DisplayObject = this.getBridgeImage();
            if (_local2 == null){
                return;
            };
            this.m_ghostBridge.addChild(_local2);
            this.m_ghostBridge.scaleX = this.bridgeItem.ghostScale;
            this.m_ghostBridge.scaleY = this.bridgeItem.ghostScale;
            var _local3:Vector2 = IsoMath.tilePosToPixelPos(this.bridgeItem.targetSquare.x, this.bridgeItem.targetSquare.y);
            this.m_ghostBridge.x = (_local3.x + this.bridgeItem.ghostXOffset);
            this.m_ghostBridge.y = (_local3.y + this.bridgeItem.ghostYOffset);
            this.m_ghostBridge.alpha = this.bridgeItem.ghostAlpha;
            if (_local1){
                GlobalEngine.viewport.overlayBase.addChild(this.m_ghostBridge);
            };
        }
        override protected function updateHighlightPoint(_arg1:MouseEvent):Boolean{
            var _local2:Boolean = super.updateHighlightPoint(_arg1);
            if (this.selectedBridge == null){
                return (false);
            };
            if (this.selectedBridge.freeExpansionZone){
                this.drawFreeWaterArea(this.selectedBridge.freeExpansionZone, EmbeddedArt.lightishBlueTextColor);
            } else {
                this.clearFreeWaterArea();
            };
            return (_local2);
        }
        override protected function isBridgePositionValid():Boolean{
            if ((((this.bridgeItem.targetSquare.x == m_highlightedPoint.x)) && ((this.bridgeItem.targetSquare.y == m_highlightedPoint.y)))){
                return (true);
            };
            return (false);
        }
        override protected function completePlaceResource(_arg1:LotSite=null):void{
            UI.popupLock();
            super.completePlaceResource(_arg1);
            UI.popupUnlock();
            Global.world.citySim.announcerManager.cycleAllAnnouncers();
        }
        public function setupGhostBridgeImage():void{
            if (this.selectedBridge == null){
                this.setupSelectedBridge();
            };
            this.drawGhostBridge();
        }
        override protected function handleObjectDrop(_arg1:MouseEvent=null):void{
            var _local2:LotSite;
            var _local4:GuideTile;
            var _local5:Vector2;
            var _local3:int;
            if (((Global.guide.isActive()) && (Global.guide.guideTiles.length))){
                m_objectPlacedSuccess = false;
                m_positionValid = (_arg1) ? this.updateHighlightPoint(_arg1) : true;
                _local3 = 0;
                while (_local3 < Global.guide.guideTiles.length) {
                    _local4 = (Global.guide.guideTiles[_local3] as GuideTile);
                    _local5 = new Vector2(_local4.origin.x, _local4.origin.y);
                    if (m_highlightedPoint.equals(_local5)){
                        if (m_positionValid){
                            m_objectPlacedSuccess = true;
                            break;
                        };
                        UI.displayStatus(ZLoc.t("Main", "MustClearSpace"), _arg1.stageX, _arg1.stageY);
                        Global.ui.showTickerMessage(ZLoc.t("Main", "TickerClearBridgeLand"));
                        return;
                    };
                    _local3++;
                };
                if (m_objectPlacedSuccess){
                    Global.guide.guideTiles.splice(_local3, 1);
                    m_selectedObject.setPosition(_local5.x, _local5.y);
                    m_selectedObject.conditionallyReattach();
                    m_selectedObject.onObjectDrop(m_objectStartPos);
                    m_selectedObject.setId(m_resource.getId());
                    this.completePlaceResource();
                    _local4.cleanup();
                    _local4 = null;
                    if ((m_selectedObject is MapResource)){
                        (m_selectedObject as MapResource).trackAction(TrackedActionType.PLACE);
                    };
                    return;
                };
            };
        }

    }
}//package GameMode 
